#include <59header.h>

/* Usage: ./10_my_ls dirname 或者 ./10_my_ls  */
int main(int argc, char *argv[]){
    char *dir_name; // 待打印的目标目录的路径名
    if(argc == 1){
        dir_name = ".";
    }else if(argc == 2){
        dir_name = argv[1];
    }else{
        fprintf(stderr, "args num error!\n");
    }

    DIR *dirp = opendir(dir_name);
    ERROR_CHECK(dirp, NULL, "opendir");
    // 切换工作目录到目标目录
    int ret = chdir(dir_name);
    ERROR_CHECK(ret, -1, "chdir");

    struct dirent* direntp;
    while((direntp = readdir(dirp)) != NULL){
        struct stat stat_buf;
        int ret = stat(direntp->d_name, &stat_buf);
        ERROR_CHECK(ret, -1, "stat");
        // 1.处理文件的类型和权限，处理成字符串然后打印
        char mode_str[1024] = {0};
        mode2str(stat_buf.st_mode, mode_str);

        // 2.将用户id和组id处理成用户名和组名字符串
        // 3.将最后修改时间的时间戳秒数处理成时间字符串
        char time_str[1024] = {0};
        time2str(stat_buf.st_mtime, time_str);

        printf("%s %lu %s %s %lu %s %s\n",
            mode_str,
            stat_buf.st_nlink,
            ,
            ,
            stat_buf.st_size,
            time_str,
            direntp->d_name
        );
    }
    closedir(dirp);
    return 0;
}   

